﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Odbc;
using System.Data.SqlClient;

namespace Btrieve_Retriever
{
    public partial class Form3 : Form
    {
        private String tableName;
        private OdbcDataReader reader;
        private int columnCount;
        private int rowCount, currentRow;

        public Form3(String title)
        {
            InitializeComponent();
            this.Text = title;
            tableName = title;
            label1.Text = "Warning: Tables are very large and will take a lot of time to fully load";
            populateTable();
        }

        private void populateTable() 
        {
            String query = "SELECT * FROM " + tableName;
            OdbcCommand command = new OdbcCommand(query, Program.myConnection);
            reader = command.ExecuteReader(CommandBehavior.SequentialAccess);

           /*Program.myConnection2.Open();
            SqlCommand sCommand = new SqlCommand("CREATE TABLE NewTable (Name char(50));",Program.myConnection2);
            sCommand.ExecuteNonQuery();
            Program.myConnection2.Close();*/

            columnCount = reader.FieldCount;
            dataGridView1.ColumnCount = columnCount;
            dataGridView1.ColumnHeadersVisible = true;

           
            for (int i = 0; i < columnCount; i++)
            {
               dataGridView1.Columns[i].Name = (reader.GetName(i));
            }

          //  populateRow(reader,columnCount);

        }

        private void populateRow()
        {
            String query2 = "SELECT COUNT(*) FROM " + tableName;
            OdbcCommand command2 = new OdbcCommand(query2, Program.myConnection);
            rowCount = (int)command2.ExecuteScalar();
            currentRow = 0; 

            string[] row = new string[columnCount];
            
            while (!reader.IsClosed)
            {
                try
                {
                    reader.Read();
                for (int i = 0; i < columnCount; i++)
                {
                    if (reader != null)
                    {
                        row[i] = reader.GetString(i);
                    }
                    
                }
                }
                catch (OdbcException exp)
                {
                    //this.Close();
                }
               // dataGridView1.Rows.Add(row);
                //dataGridView1.Update();
                
                currentRow++;
                double perc = ((double)currentRow / rowCount) * 100.0;
                int percentageComplete = Convert.ToInt32(perc);
                Console.WriteLine("          " + currentRow + "\\" + rowCount);
                backgroundWorker1.ReportProgress(percentageComplete,row);
            }
           // label1.Text = "Loading complete    x";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            progressBar1.Visible = true;
           // label1.Visible = false;
            button1.Visible = false;
            backgroundWorker1.RunWorkerAsync();
            //populateRow();
        }

        private void label1_Click(object sender, EventArgs e)
        {
            label1.Visible = false;
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            Console.WriteLine("PROGRESS: " + currentRow + "\\" + rowCount + " " + e.ProgressPercentage);
            string[] row = (string[])e.UserState;
            progressBar1.Value = e.ProgressPercentage;
            label1.Text = "Loaded " + e.ProgressPercentage + "% (" + currentRow + "\\" + rowCount + ") of the file";
            dataGridView1.Rows.Add(row);
            // dataGridView1.Update();
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            progressBar1.Visible = false;
            label1.Visible = false;
            reader.Close();
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            populateRow();
        }

        private void Form3_Load(object sender, EventArgs e)
        {

        }



        private void Form3_FormClosed(object sender, FormClosedEventArgs e)
        {
            Console.WriteLine("CLOSED");
            reader.Close();
        }

    }
}
